package com.example.a_lib;


import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.example.a_lib.http.IAPICallBack;
import com.example.a_lib.http.resp.Response;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


public abstract class SmartObserver<T> implements Observer<Response<T>>, IAPICallBack<Response<T>> {
    Context context;
    /**
     * false时，即pageManager托管给SmartObserver处理
     */
    boolean 是否需要在onsuccess中操作pageManager = true;

    public SmartObserver(Context context) {
        this.context = context;
    }

    public SmartObserver() {
    }

    @Override
    public void onRequestStart() {
    }


    @Override
    public void onRequestEnd() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();
    }


    @Override
    public void onNext(Response<T> basebean) {
        onRequestEnd();
        if (basebean.isSuccess()) {
            try {
                //    pageManager会影响布局结构，如果遇到布局错乱（onSuccess中动态修改布局高度、padding、margin无效的问题）
                //    应在 onSuccess处理之前showContent还原布局结构（通过设置 【是否需要在onsuccess中操作pageManager】为true，在 onSuccess的回调中手动showContent）

                // public void onSuccess(BaseBean<CommunityHomeBean> bean) {
                //                        getDefaultActvPageManager().showContent();
                //                        initView();
                //
                //                    }
                onSuccess(basebean);
            } catch (Exception e) {
                e.printStackTrace();
                onError(e);
            }
        } else { //服务器返回的code不是200,
            onFailure(basebean.getCode(), basebean.getMsg() == null ? "" : basebean.getMsg());
        }


    }

    @Override
    public void onError(Throwable e) {
        onRequestEnd();
        String errorMsg;
        if (e instanceof SocketTimeoutException) {  //连接超时
            errorMsg = "连接超时";
        } else if (e instanceof ConnectException) {
            errorMsg = "请检查网络";
        } else if (e instanceof UnknownHostException) {
            errorMsg = "请检查网络";
        } else if (e instanceof UnknownServiceException) {
            errorMsg = "未知的服务器错误";
        } else if (e instanceof SocketException) {
            errorMsg = "读取网络数据失败";
        } else if (e instanceof IOException) {
            errorMsg = "读取网络数据失败";
        } else if (e instanceof HttpException) {
            errorMsg = "服务器繁忙";
        } else if (e instanceof NullPointerException) {
            errorMsg = "加载出错了";
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            errorMsg = "加载出错了";
        } else if (e instanceof IllegalArgumentException) {
            errorMsg = "加载出错了";
        } else {
            errorMsg = e.getMessage();
        }
        Log.e("http", "onError: " + e.getCause());
        e.printStackTrace();
        onFailure(-1, errorMsg);

    }

    @Override
    public void onComplete() {
    }

//    /**
//     * 返回成功
//     *
//     * @param bean
//     */
//    @Override
//    public void onSuccess(BaseBean<T> bean) {
//    }


    //code -1是本地定义值，其余值为服务器返回值。重写的话
    @Override
    public void onFailure(int code, String errorMsg) {
        disposeEorCode(code, errorMsg);
    }

    public static boolean isCanJump = true;

    private void disposeEorCode(int code, String message) {
        LogUtils.eTag("tag", "disposeEorCode");


    }


}
